package cn.hairui.fastms.core.controller;


import cn.hairui.fastms.base.cache.CacheManage;
import cn.hairui.fastms.base.config.WebLogInfo;
import cn.hairui.fastms.constant.FieldConstant;
import cn.hairui.fastms.constant.GlobalConstant;
import cn.hairui.fastms.core.bean.SRoleResc;
import cn.hairui.fastms.core.bean.SUser;
import cn.hairui.fastms.core.service.SRoleRescService;
import cn.hairui.fastms.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色资源关系表 前端控制器
 * </p>
 *
 * @author lihairui
 * @since 2020-11-29
 */
@Controller
@RequestMapping("/sRoleResc")
public class SRoleRescController {
    @Autowired
    private SRoleRescService sRoleRescService;

    @WebLogInfo(description = "角色资源信息列表页面")
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request) {
        return "pages/core/sroleresc/sroleresc";
    }

    @WebLogInfo(description = "获取角色资源菜单")
    @RequestMapping(value = "/menu")
    @ResponseBody
    public String menu(HttpServletRequest request, String roleCde, String menuId) {
        if (roleCde == null) {
            return null;
        }
        List<Map> list = new ArrayList<>();
        if (menuId == null || "".equals(menuId)) {
            menuId = GlobalConstant.PARENT_RESC_ID_MAX;
        }
        list = sRoleRescService.selectRoleRescByRoleCde(roleCde, menuId);

        String result = "";
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            String status = "";
            String statusText = "";
            if (map.get("RESC_URL") != null && !"".equals(map.get("RESC_URL"))) {
                status = "open";
                statusText = "子节点";
            } else {
                status = "closed";
                statusText = "父节点";
            }
            String url = "";
            if (map.get("RESC_URL") != null) {
                url = (String) map.get("RESC_URL");
            }
            result = result + "  {\n" +
                    "    \"id\": \"" + map.get("RESC_CDE") + "\",\n" +
                    "    \"pid\": \"" + map.get("RESC_PARENTID") + "\",\n" +
                    "    \"text\": \"" + map.get("RESC_DESC") + "\",\n" +
                    "    \"state\": \"" + status + "\",\n" +
                    "    \"stateText\": \"" + statusText + "\",\n" +
                    "    \"iconCls\": \"" + map.get("RESC_ICON") + "\",\n" +
                    "    \"grantSts\": \"" + map.get("GRANT_STS") + "\",\n" +
                    "    \"roleCde\": \"" + map.get("ROLE_CDE") + "\",\n" +
                    "    \"url\": \"" + url + "\"\n";
            if (i + 1 == list.size()) {
                result = result + "  }";
            } else {
                result = result + "  },\n";
            }
        }
        return "[\n" + result + "]";
    }

    @WebLogInfo(description = "角色资源信息数据新增保存")
    @RequestMapping(value = "/save")
    @ResponseBody
    public String save(HttpServletRequest request, String flag, String roleCde, String[] rescCde) {
        SUser currentUser = (SUser) request.getSession().getAttribute(GlobalConstant.GLOBAL_SESSION_NAME);
        if (GlobalConstant.OPERATION_ADD.equals(flag)) {
            //新增保存
            if (roleCde == null || rescCde == null) {
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0001).toJSONString();
            }
            if (rescCde.length > 0) {
                for (int i = 0; i < rescCde.length; i++) {
                    String rescCdeValue = rescCde[i];
                    //判断是否已经存在
                    if (sRoleRescService.selectRoleRescByRoleCdeAndRescCde(roleCde, rescCdeValue) != null) {
                        //已存在 更新
                        SRoleResc sRoleResc = new SRoleResc();
                        sRoleResc.setRoleCde(roleCde);
                        sRoleResc.setRescCde(rescCdeValue);
                        sRoleResc.setUpdateTime(DateUtil.getCurrentDateTimeStr());
                        sRoleResc.setUpdateUser(currentUser.getUserAccount());
                        UpdateWrapper<SRoleResc> updateWrapper = new UpdateWrapper<>();
                        updateWrapper
                                .eq(FieldConstant.ROLE_CDE[0], roleCde)
                                .eq(FieldConstant.RESC_CDE[0], rescCdeValue)
                                .set(FieldConstant.UPDATE_USER[0], currentUser.getUserAccount())
                                .set(FieldConstant.UPDATE_TIME[0], DateUtil.getCurrentDateTimeStr());

                        sRoleRescService.update(null, updateWrapper);
                    } else {
                        //不存在用户 允许新增
                        SRoleResc sRoleResc = new SRoleResc();
                        sRoleResc.setRoleCde(roleCde);
                        sRoleResc.setRescCde(rescCdeValue);
                        sRoleResc.setUpdateTime(DateUtil.getCurrentDateTimeStr());
                        sRoleResc.setUpdateUser(currentUser.getUserAccount());
                        sRoleRescService.save(sRoleResc);
                    }
                }
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
            } else {
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0001).toJSONString();
            }
        } else if (GlobalConstant.OPERATION_REMOVE.equals(flag)) {
            //删除记录
            if (roleCde == null || rescCde == null) {
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0001).toJSONString();
            }
            if (rescCde.length > 0) {
                for (int i = 0; i < rescCde.length; i++) {
                    String rescCdeValue = rescCde[i];
                    //通过rescCde和roleCde进行查找删除
                    UpdateWrapper<SRoleResc> updateWrapper = new UpdateWrapper<>();
                    updateWrapper
                            .eq(FieldConstant.ROLE_CDE[0], roleCde)
                            .eq(FieldConstant.RESC_CDE[0], rescCdeValue);
                    sRoleRescService.remove(updateWrapper);
                }
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
            } else {
                return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0001).toJSONString();
            }
        } else {
            return null;
        }
    }

    @WebLogInfo(description = "角色资源信息数据删除")
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Transactional
    public String delete(HttpServletRequest request) {
        String roleCde = request.getParameter("roleCde");
        String rescCde = request.getParameter("rescCde");
        QueryWrapper<SRoleResc> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(FieldConstant.ROLE_CDE[0], roleCde).eq(FieldConstant.RESC_CDE[0], rescCde);
        sRoleRescService.remove(queryWrapper);
        return CacheManage.msgMap.get(GlobalConstant.MSG_CDE_S0000).toJSONString();
    }
}

